package com.wjw.service;

import com.wjw.mapper.RoadTechnicalEvaluateTableMapper;
import com.wjw.model.Road_technical_evaluate_table;
import com.wjw.utils.GetSqlSessionUtils;
import com.wjw.viewmodel.Road_patrol_table_view;
import com.wjw.viewmodel.Road_technical_evaluate_table_view;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoadTechnicalEvaluateTableService {


//    private SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
//    private RoadTechnicalEvaluateTableMapper mapper=sqlsession.getMapper(RoadTechnicalEvaluateTableMapper.class);


    public List<Road_technical_evaluate_table_view> findAll(){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadTechnicalEvaluateTableMapper mapper=sqlsession.getMapper(RoadTechnicalEvaluateTableMapper.class);
        try {
            List<Road_technical_evaluate_table> all = mapper.findAll();
            List<Road_technical_evaluate_table_view> list= new ArrayList<Road_technical_evaluate_table_view>();
            for(Road_technical_evaluate_table e:all){
                Road_technical_evaluate_table_view k = new Road_technical_evaluate_table_view();
                k.setRoad_basic_name(mapper.findRoadById(e.getRoad_basic_id()).getRoad_basic_name());
                k.setRoad_technical_evaluate_date(e.getRoad_technical_evaluate_date());
                k.setRoad_technical_evaluate_id(e.getRoad_technical_evaluate_id());
                k.setRoad_technical_evaluate_PCI(e.getRoad_technical_evaluate_PCI());
                k.setRoad_technical_evaluate_PCIindex(e.getRoad_technical_evaluate_PCIindex());
                k.setRoad_technical_evaluate_PQI(e.getRoad_technical_evaluate_PQI());
                k.setRoad_technical_evaluate_PQIindex(e.getRoad_technical_evaluate_PQIindex());
                k.setRoad_technical_evaluate_RQI_IRI(e.getRoad_technical_evaluate_RQI_IRI());
                k.setRoad_technical_evaluate_RQIindex(e.getRoad_technical_evaluate_RQIindex());
                k.setRoadway_id(e.getRoadway_id());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public List<Road_technical_evaluate_table_view> findByPage(int page,int limit){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadTechnicalEvaluateTableMapper mapper=sqlsession.getMapper(RoadTechnicalEvaluateTableMapper.class);
        try {
            int start=(page-1)*limit;
            List<Road_technical_evaluate_table> all = mapper.findByPage(start,limit);
            List<Road_technical_evaluate_table_view> list= new ArrayList<Road_technical_evaluate_table_view>();
            for(Road_technical_evaluate_table e:all){
                Road_technical_evaluate_table_view k = new Road_technical_evaluate_table_view();
                k.setRoad_basic_name(mapper.findRoadById(e.getRoad_basic_id()).getRoad_basic_name());
                k.setRoad_technical_evaluate_date(e.getRoad_technical_evaluate_date());
                k.setRoad_technical_evaluate_id(e.getRoad_technical_evaluate_id());
                k.setRoad_technical_evaluate_PCI(e.getRoad_technical_evaluate_PCI());
                k.setRoad_technical_evaluate_PCIindex(e.getRoad_technical_evaluate_PCIindex());
                k.setRoad_technical_evaluate_PQI(e.getRoad_technical_evaluate_PQI());
                k.setRoad_technical_evaluate_PQIindex(e.getRoad_technical_evaluate_PQIindex());
                k.setRoad_technical_evaluate_RQI_IRI(e.getRoad_technical_evaluate_RQI_IRI());
                k.setRoad_technical_evaluate_RQIindex(e.getRoad_technical_evaluate_RQIindex());
                k.setRoadway_id(e.getRoadway_id());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public boolean add(Road_technical_evaluate_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadTechnicalEvaluateTableMapper mapper=sqlsession.getMapper(RoadTechnicalEvaluateTableMapper.class);
        try {
            Road_technical_evaluate_table k = new Road_technical_evaluate_table();
            k.setRoad_basic_id(mapper.findRoadByName(e.getRoad_basic_name()).getRoad_basic_id());
            k.setRoad_technical_evaluate_date(e.getRoad_technical_evaluate_date());
            k.setRoad_technical_evaluate_id(e.getRoad_technical_evaluate_id());
            k.setRoad_technical_evaluate_PCI(e.getRoad_technical_evaluate_PCI());
            k.setRoad_technical_evaluate_PCIindex(e.getRoad_technical_evaluate_PCIindex());
            k.setRoad_technical_evaluate_PQI(e.getRoad_technical_evaluate_PQI());
            k.setRoad_technical_evaluate_PQIindex(e.getRoad_technical_evaluate_PQIindex());
            k.setRoad_technical_evaluate_RQI_IRI(e.getRoad_technical_evaluate_RQI_IRI());
            k.setRoad_technical_evaluate_RQIindex(e.getRoad_technical_evaluate_RQIindex());
            k.setRoadway_id(e.getRoadway_id());
            mapper.add(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean delete(int id){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadTechnicalEvaluateTableMapper mapper=sqlsession.getMapper(RoadTechnicalEvaluateTableMapper.class);
        try {
            mapper.delete(id);
            sqlsession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean change(Road_technical_evaluate_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadTechnicalEvaluateTableMapper mapper=sqlsession.getMapper(RoadTechnicalEvaluateTableMapper.class);
        try {
            Road_technical_evaluate_table k = new Road_technical_evaluate_table();
            k.setRoad_basic_id(mapper.findRoadByName(e.getRoad_basic_name()).getRoad_basic_id());
            k.setRoad_technical_evaluate_date(e.getRoad_technical_evaluate_date());
            k.setRoad_technical_evaluate_id(e.getRoad_technical_evaluate_id());
            k.setRoad_technical_evaluate_PCI(e.getRoad_technical_evaluate_PCI());
            k.setRoad_technical_evaluate_PCIindex(e.getRoad_technical_evaluate_PCIindex());
            k.setRoad_technical_evaluate_PQI(e.getRoad_technical_evaluate_PQI());
            k.setRoad_technical_evaluate_PQIindex(e.getRoad_technical_evaluate_PQIindex());
            k.setRoad_technical_evaluate_RQI_IRI(e.getRoad_technical_evaluate_RQI_IRI());
            k.setRoad_technical_evaluate_RQIindex(e.getRoad_technical_evaluate_RQIindex());
            k.setRoadway_id(e.getRoadway_id());
            mapper.change(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }
}
